﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Model.Manager;
using Enterprise.Data.Manager;
using Enterprise.Infrastructure;
namespace Enterprise.Service.Manager
{
    /// <summary>
    /// 经理信箱服务
    /// </summary>
    public class MsgService
    {

        /// <summary>
        /// 用户缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-Msg";

        /// <summary>
        /// 经理信箱列表 返回所有留言
        /// </summary>
        /// <returns></returns>
        private List<MsgEntity> MsgList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<MsgEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                MsgData data = new MsgData();
                List<MsgEntity> list = data.MsgList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 留言显示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MsgEntity MsgDisp(int id)
        {
            return MsgList().Where(p => p.MsgId == id).FirstOrDefault();
        }

        /// <summary>
        /// 汇总留言列表
        /// 规则：
        ///  1、不回复不公开，留言人和回复人可以看
        ///  2、回复后是否可以公开，取决于回复人的设置，设置为不公开时，留言人和回复人可以看
        /// </summary>
        /// <returns></returns>
        public List<MsgEntity> MsgListByShow()
        {
            List<MsgEntity> relist = new List<MsgEntity>();            
            MsgReplyService rService = new MsgReplyService();
            var q = MsgList().OrderByDescending(p => p.MsgCreateTime);
            foreach (MsgEntity _msg in q)
            {
                //把所有已经回复的留言内容
                List<MsgReplyEntity> replylist = rService.MsgReplyList(_msg.MsgId);
                if (replylist.Count>0)
                {
                    MsgReplyEntity mr = replylist.OrderByDescending(p => p.ReplyTime).FirstOrDefault();
                    if (mr != null)
                    {
                        if (Utility.Get_UserId == _msg.MsgFUser || mr.ReplyIsShow == 1)
                        {
                            relist.Add(_msg);
                        }
                    }
                }
            }
            return relist;
        }

        /// <summary>
        /// 收到的所有留言
        /// </summary>
        /// <returns></returns>
        public List<MsgEntity> MsgListByOwn()
        {
            return MsgList().Where(p => p.MsgTUser == Utility.Get_UserId).OrderByDescending(p => p.MsgCreateTime).ToList();
        }

        /// <summary>
        /// 经理信箱留言操作 添加、修改、删除
        /// </summary>
        /// <param name="entity">经理信箱实体</param>
        public void MsgExecute(MsgEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into MANAGER_MSG(MSGID,MSGTITLE,MSGCONTENT,MSGFUSER,MSGTUSER,MSGISSHOW,MSGISANONYMOUS,MSGFIP,MSGCREATETIME) values(SEQ_MANAGER_MSG.nextval,'" + entity.MsgTitle + "','" + entity.MsgContent + "'," + entity.MsgFUser + "," + entity.MsgTUser + "," + entity.MsgIsShow + "," + entity.msgIsAnonymous + ",'" + entity.MsgFIp + "'," + Utility.FormatOracleDate(entity.MsgCreateTime) + ")";
                    break;
                case "Update":
                    cmd = "Update MANAGER_MSG set MSGTITLE='" + entity.MsgTitle + "',MSGCONTENT='" + entity.MsgContent + "' , MSGFUSER=" + entity.MsgFUser + " , MSGTUSER=" + entity.MsgTUser + " , MSGISSHOW=" + entity.MsgIsShow + " , MSGISANONYMOUS=" + entity.msgIsAnonymous + ",MSGFIP='"+entity.MsgFIp+"' where MSGID=" + entity.MsgId;
                    break;
                case "Delete":
                    cmd = "Delete from MANAGER_MSG where MSGID=" + entity.MsgId;
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                MsgExecuteBySql(cmd);
            }
        }

        /// <summary>
        /// 经理信箱操作  
        /// </summary>
        /// <param name="cmd">sql语句</param>
        public void MsgExecuteBySql(string cmd)
        {
            MsgData data = new MsgData();
            data.MsgExecute(cmd);
            CacheHelper.RemoveCache(cacheKey);
        }

    }
}
